/**
 * Created by ChenYong on 2017-05-23.
 *
 * 学习统计。
 */

$(function () {
    /** 年份的 jQuery 对象 */
    var $year = $("#year");
    /** 月份的 jQuery 对象 */
    var $month = $("#month");
    /** 搜索按钮的 jQuery 对象 */
    var $searchBtn = $("#search_btn");
    /** 学习统计图表的 jQuery 对象 */
    var $learnCountChart = $("#learn_count_chart");
    /** 学习日期的 jQuery 对象 */
    var $learnDate = $("#learn_date");
    /** 学习人的 jQuery 对象 */
    var $learnPerson = $("#learn_person");
    /** 搜索学习人按钮的 jQuery 对象 */
    var $searchLearnPersonBtn = $("#search_learn_person_btn");
    /** 学习统计图表 */
    var learnCountChart = echarts.init($learnCountChart.get(0));
    /** 学习统计图表的配置 */
    var learnCountChartOption = {
        title: {
            x: "center",
            text: "",
            textStyle: {
                fontSize: 20
            }
        },
        tooltip: {
            trigger: "axis",
            axisPointer: {
                type: "shadow"
            }
        },
        grid: {
            left: "1%",
            right: "1%",
            bottom: "8%",
            containLabel: true
        },
        legend: {
            data: ["学习人数"],
            left: "center",
            bottom: "bottom"
        },
        xAxis: [{
            data: [],
            type: "category",
            axisTick: {
                alignWithLabel: true
            }
        }],
        yAxis: [{
            type: "value"
        }],
        series: [{
            name: "学习人数",
            data: [],
            type: "bar",
            label: {
                normal: {
                    show: true,
                    position: "top"
                }
            }
        }]
    };
    /** 学习人列表的表格，DataTables 的 API 对象 */
    var learnPersonTable = null;
    /** 当前日期 */
    var currDate = new Date();
    /** 学习日期字符串 */
    var learnDate = ($year.val() + "-" + $month.val() + "-" + (((currDate.getDate() + "").length == 1) ? ("0" + currDate.getDate()) : (currDate.getDate() + "")));

    // 点击柱状图
    learnCountChart.on("click", function (params) {
        // 日
        var dayStr = params.name.substring(0, params.name.lastIndexOf("日"));

        if (dayStr.length === 1) {
            dayStr = "0" + dayStr;
        }

        learnDate = ($year.val() + "-" + $month.val() + "-" + dayStr);

        $learnPerson.val("");

        searchData();
    });

    // 点击搜索
    $searchBtn.click(function () {
        $learnPerson.val("");

        learnDate = ($year.val() + "-" + $month.val() + "-01");

        getLearnCount();
        searchData();
    });

    // 点击搜索学习人
    $searchLearnPersonBtn.click(function () {
        searchData();
    });

    // 展示学习人列表的表格
    learnPersonTable = $("#learn_person_table").myDataTable({
        url: "web/learn/person/list.json",
        sScrollY: "450px",
        sScrollX: true,
        sScrollXInner: "100%",
        aaSorting: [[0, "desc"]],
        aLengthMenu: [10, 30, 50, 100],
        aoColumns: [{
            mData: "loginName",
            sName: "loginName",
            sTitle: "学习人",
            sWidth: "300px"
        }, {
            mData: "loginid",
            sName: "loginid",
            sTitle: "账号",
            sWidth: "300px"
        }, {
            mData: "learnTime",
            sName: "learnTime",
            sTitle: "学习时间"
        }, {
            mData: "operator",
            sTitle: "操作",
            sWidth: "100px",
            bSortable: false,
            bSearchable: false,
            sDefaultContent: "<div class='hidden-sm hidden-xs action-buttons'></div>"
        }],
        beforeSendFn: function () {
            // 禁用搜索按钮
            $searchLearnPersonBtn.html("<i class='fa fa-spinner fa-spin'></i>正在搜索…").attr({"disabled": "disabled"});
        },
        completeFn: function () {
            // 启用搜索按钮
            $searchLearnPersonBtn.html("<i class='ace-icon iconfont icon-search f-s-12'></i>搜索").removeAttr("disabled");
        },
        successFn: function () {
            $learnDate.html(learnDate);
        }
    });

    getLearnCount();

    /**
     *
     * 获取学习统计。
     *
     */
    function getLearnCount() {
        $.myAjax({
            url: "web/learn/count.json",
            data: {
                learnMonth: ($year.val() + "-" + $month.val())
            },
            beforeSendFn: function () {
                // 禁用搜索按钮
                $searchBtn.html("<i class='fa fa-spinner fa-spin'></i>正在搜索…").attr({"disabled": "disabled"});
            },
            completeFn: function () {
                // 启用搜索按钮
                $searchBtn.html("<i class='ace-icon iconfont icon-search f-s-12'></i>搜索").removeAttr("disabled");
            },
            successFn: function (data) {
                var data1 = new Array();
                data1.push(new Array());
                data1.push(new Array());

                $.each(data.results[1], function (i, e) {
                    data1[0].push(e.learnTime);
                    data1[1].push(e.learnPersons);
                });

                learnCountChartOption.title.text = (data.results[0] + "学习统计");
                learnCountChartOption.xAxis[0].data = data1[0];
                learnCountChartOption.series[0].data = data1[1];

                learnCountChart.setOption(learnCountChartOption);
            }
        });
    }

    /**
     * 搜索数据。
     */
    function searchData() {
        // 学习日期
        if ($.isBlank(learnDate)) {
            $.toast({
                top: 5,
                msg: "学习日期不能为空",
                type: "warn",
                close: 3000
            });

            return;
        }

        // 学习人
        if ($.isNotBlank($learnPerson.val()) && ($learnPerson.val().length > 50)) {
            $.toast({
                top: 5,
                msg: "学习人的长度不能超过50个字",
                type: "warn",
                close: 3000
            });

            return;
        }

        // 搜索开始…
        if ($.isNotNull(learnPersonTable)) {
            learnPersonTable.myAjaxData = {
                learnDate: learnDate,
                learnPerson: $learnPerson.val()
            };

            // 刷新动态表格（分页重置）
            learnPersonTable.ajax.reload();
        }
    }
});